<?php

/**
 * Description of Html
 *
 */
class Html extends CHtml {

	public static function buttonInGrid($id, $url, $titleHover, $titlePopup, $class, $dialogWidth, $dialogHeight, $extraSettings = array()) {
		$arrayDefault = array(
            'id' => $id,
            'title' => $titleHover,
            'class' => $class,
            'data-update-dialog-title' => $titlePopup,
            'href' => $url,
            'dialog-width' => $dialogWidth,
            'dialog-height' => $dialogHeight);
		if (count($extraSettings) == 0)
		return CHtml::button('', $arrayDefault);
		else {
			$result = array_merge($arrayDefault, $extraSettings);
			return CHtml::button('', $result);
		}
	}

	public static function linkPopUpInGrid($id, $url, $titleHover, $titlePopup, $class, $dialogWidth, $dialogHeight, $extraSettings = array()) {
		$arrayDefault = array(
            'id' => $id,
            'class' => $class,
            'data-update-dialog-title' => $titlePopup,
            'href' => $url,
            'dialog-width' => $dialogWidth,
            'dialog-height' => $dialogHeight);
		if (count($extraSettings) == 0)
		return CHtml::link($titleHover, $url, $arrayDefault);
		else {
			$result = array_merge($arrayDefault, $extraSettings);
			return CHtml::button($titleHover, $url, $result);
		}
	}
	
	public static function buttonPopup($id, $text, $url, $titleHover, $titlePopup, $class, $dialogWidth, $dialogHeight, $extraSettings = array()) {
		$arrayDefault = array(
            'id' => $id,
            'title' => $titleHover,
            'class' => $class,
            'data-update-dialog-title' => $titlePopup,
            'href' => $url,
            'dialog-width' => $dialogWidth,
            'dialog-height' => $dialogHeight,
            'value' => $text);
		if (count($extraSettings) == 0)
		return CHtml::button('', $arrayDefault);
		else {
			$result = array_merge($arrayDefault, $extraSettings);
			return CHtml::button('', $result);
		}
	}

	public static function linkPopup($id, $text, $url, $titleHover, $titlePopup, $dialogWidth, $dialogHeight, $class = 'update-dialog-open-link') {
		$htmlOptions = array(
            'id' => $id,
            'title' => $titleHover,
            'class' => $class,
            'data-update-dialog-title' => $titlePopup,
            'dialog-width' => $dialogWidth,
            'dialog-height' => $dialogHeight);

		return CHtml::link($text, $url, $htmlOptions);
	}
	
	public static function buttonConfirm($text, $name, $confirm = 'ok', $extraSettings = array()) {
		$class = '';
		if ($confirm == 'ok')
		$class = 'button ok update-dialog-submit-button';
		else if ($confirm == 'cancel')
		$class = 'button cancel update-dialog-cancel-button';
		$arrayDefault = array(
            'name' => $name,
            'class' => $class);
		if (count($extraSettings) == 0)
		return CHtml::htmlButton($text, $arrayDefault);
		else {
			$result = array_merge($arrayDefault, $extraSettings);
			return CHtml::htmlButton($text, $result);
		}
	}

	public static function submitConfirm($text, $name, $confirm = 'ok', $extraSettings = array()) {
		$class = 'button';

		$arrayDefault = array(
            'id' => 'viewclient',
            'name' => $name,
            'class' => $class);
		if (count($extraSettings) == 0)
		return CHtml::htmlButton($text, $arrayDefault);
		else {
			$result = array_merge($arrayDefault, $extraSettings);
			return CHtml::htmlButton($text, $result);
		}
	}

	public static function buttonFilter() {
		Yii::app()->clientScript->registerScript('search', "
            var startTime = 0;
            $('.search-form form').submit(function(){
                var submitTime = new Date().getTime();
                if((submitTime - startTime) < 1000){
                    return false;
                }
                startTime = new Date().getTime();
                $.fn.yiiGridView.update('DataGrid', {
                    data: $(this).serialize()
                });
                return false;
            });
        ");
		return self::submitButton(t('BTN_FILTER'), array('id' => 'btnFilter', 'name' => 'btnFilter', 'class' => 'button filter'));
	}

	public static function buttonPopupFilter() {
		Yii::app()->clientScript->registerScript('searchPopup', "
            var startTime = 0;
            $('#frm-search-popup').submit(function(){	    
                var submitTime = new Date().getTime();
                if((submitTime - startTime) < 1000){
                    return false;
                }
                startTime = new Date().getTime();
                $.fn.yiiGridView.update('DataGridPopup', {
                    data: $(this).serialize()
                });
                return false;
            });
        ");
		return self::submitButton(t('BTN_FILTER'), array('id' => 'btnFilterPopup', 'name' => 'btnFilterPopup', 'class' => 'button filter', 'style' => 'font: bold 11px sans-serif;'));
	}

	public static function buttonPopupFilterCommon($dataGridId = 'DataGridPopup') {
		Yii::app()->clientScript->registerScript('searchPopupCommon', "
            var startTime = 0;
            $('#frm-search-popup').submit(function(){
                var submitTime = new Date().getTime();
                if((submitTime - startTime) < 1000){
                    return false;
                }
                startTime = new Date().getTime();		
                $.fn.yiiGridView.update('$dataGridId', {
                    data: $(this).serialize()
                });
                return false;
            });
        ");
		return self::submitButton(t('BTN_FILTER'), array('id' => 'btnFilterPopup', 'name' => 'btnFilterPopup', 'class' => 'button filter', 'style' => 'font: bold 11px sans-serif;'));
	}

	/**
	 * Generates a push button that can submit the current form in POST method.
	 * @param string the button label
	 * @param mixed the URL for the AJAX request. If empty, it is assumed to be the current URL. See {@link normalizeUrl} for more details.
	 * @param array AJAX options (see {@link ajax})
	 * @param array additional HTML attributes. Besides normal HTML attributes, a few special
	 * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
	 * @return string the generated button
	 */
	public static function ajaxSubmitButton($label, $url, $ajaxOptions = array(), $htmlOptions = array()) {
		$ajaxOptions['type'] = 'POST';
		$htmlOptions['live'] = false;
		return self::ajaxButton($label, $url, $ajaxOptions, $htmlOptions);
	}

	public static function linkCheckOutInGrid($id, $title, $checkout = 0) {
		if ($checkout != Yii::app()->user->id && $checkout != 0) {
			return "<span class='locked' id='" . $id . "'></span>" . $title;
		} else {
			$controller = Yii::app()->controller->id;
			$url = url($controller . "/update", array('id' => $id));
			return CHtml::link($title, $url, array(
                        'id' => 'linkEdit',
                        'title' => 'edit',
                        'class' => '',
			)
			);
		}
	}

	//client
	public static function linkInGrid($title, $url, $class) {
		return CHtml::link('&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp', $url, array(
                    'id' => 'link' . $title,
                    'title' => $title,
                    'class' => $class,
                    'style' => 'text-decoration: none',
		)
		);
	}

	public static function active($id, $status, $action, $titleActive = 'Published', $titleInActive = 'Unpublished', $classActive = 'icon active', $classInActive = 'icon inactive') {
		$controller = Yii::app()->controller->id;
		return CHtml::button('', array(
                    'id' => $action . $id,
                    'class' => $status == 1 ? $classActive : $classInActive,
                    'title' => $status == 1 ? $titleActive : $titleInActive,
                    'onclick' => 'LGCMS.ajaxCallback("' . url($controller . "/" . $action, array('id' => $id)) . '")',
		));
	}

	public static function activeStatus($id, $status, $action, $titleActive = 'Published', $titleInActive = 'Unpublished', $classActive = 'icon checked', $classInActive = 'icon unchecked') {
		return CHtml::button('', array(
                    'id' => $action . $id,
                    'class' => $status == 1 ? $classActive : $classInActive,
                    'title' => $status == 1 ? $titleActive : $titleInActive,
                    'style' => 'cursor:auto'
                    ));
	}

	public static function activeField($id, $status, $action, $titleActive = 'Published', $titleInActive = 'Unpublished', $classActive = 'icon active', $classInActive = 'icon inactive', $column_name = "", $isRequestField = TRUE) {
		$requiredfields = $isRequestField ? LGHelpers::getRequiredFields() : LGHelpers::getRequiredIssueFields();
		if (in_array($column_name, $requiredfields)) {
			return Html::activeStatus($id, $status, $action, $titleActive, $titleInActive, 'icon checked', 'icon unchecked');
		}
		return Html::active($id, $status, $action, $titleActive, $titleInActive, $classActive, $classInActive);
	}

	public static function defaultPage($id, $default, $action, $titleYes = 'Default', $titleNo = 'Set Default', $classYes = 'icon default_yes', $classNo = 'icon default_no') {
		$controller = Yii::app()->controller->id;
		if ($default == LGConstant::DEFAULT_PAGE_NO) {
			return CHtml::button('', array(
                        'id' => $action . $id,
                        'class' => $classNo,
                        'title' => $titleNo,
                        'onclick' => 'LGCMS.ajaxCallback("' . url($controller . "/" . $action, array('id' => $id)) . '")',
			));
		} else {
			return CHtml::button('', array(
                        'id' => $action . $id,
                        'class' => $classYes,
                        'title' => $titleYes,
			));
		}
	}

	public static function buttonPlus($id, $onclick) {
		$html = CHtml::button('', array('id' => 'plus' . $id, 'type' => 'button', 'class' => 'icon plus', 'onclick' => '%s',));
		$html = sprintf($html, $onclick);
		return $html;
	}

	public static function buttonMinus($id, $onclick) {
		$html = CHtml::button('', array('id' => 'minus' . $id, 'type' => 'button', 'class' => 'icon minus', 'onclick' => '%s',));
		$html = sprintf($html, $onclick);
		return $html;
	}

	public static function dropDownListPageSize($gridId, $pageSize) {
		return CHtml::dropDownList('pageSize', $pageSize, array(0 => 'All', 5 => 5, 10 => 10, 15 => 15, 20 => 20, 30 => 30, 50 => 50, 100 => 100), array('onchange' => "$.fn.yiiGridView.update('" . $gridId . "',{ data:{pageSize: $(this).val() }})", 'class' => 'page-size'));
	}

	public static function dataType($type) {
		$all = LGHelpers::getDataType();
		return $all[$type - 1]['name'];
	}

	public static function controlType($type, $name, $data) {
		$all = LGHelpers::getControlType();
		$controlType = $all[$type - 1]['name'];
		if ($controlType == ControlTypeString::COMBOBOX || $controlType == ControlTypeString::RADIOBUTTON || $controlType == ControlTypeString::CHECKBOX) {
			$arr = explode(',', $data);
			$divContent = Yii::app()->controller->renderPartial('_field_data', array(
                'array' => $arr, 'name' => $name), true, false);

			return Html::link($controlType, '', array('class' => 'showPopup', 'style' => 'cursor:pointer', 'dialog-width' => 300, 'dialog-height' => 400, 'dialog-title' => 'Data Source')) . $divContent;
		}
		return $controlType;
	}

	public static function customField($id, $status, $action, $titleActive = 'Custom Field', $titleInActive = 'Basic Field', $classActive = 'icon checked', $classInActive = 'icon unchecked') {
		return CHtml::button('', array(
                    'id' => $action . $id,
                    'class' => $status == 1 ? $classActive : $classInActive,
                    'title' => $status == 1 ? $titleActive : $titleInActive,
                    'style' => 'cursor:auto'
                    ));
	}

	public static function comments($requestid) {
		return Html::linkPopup('lnkComments', 'View', url('/request/note', array('id' => $requestid, 'type' => LGConstant::REQUEST_COMMENT)), 'Comments', 'Comments', 500, 500);
	}

	public static function getSearchStatusDropDownList($form, $model, $attribute, $htmlOptions = array()) {
		$listData = CHtml::listData(LGHelpers::getSearchStatus(), 'id', 'name');

		if ($htmlOptions == null) {
			$htmlOptions = array();
		}

		$htmlOptions = array_merge($htmlOptions, array(
	    'empty' => t('DEFAULT_STATUS')
		));
		return $form->dropDownList($model, $attribute, $listData, $htmlOptions);
	}

	public static function getUpdateStatusDropDownList($form, $model, $attribute, $htmlOptions = array()) {
		$listData = CHtml::listData(LGHelpers::getSearchStatus(), 'id', 'name');

		if ($htmlOptions == null) {
			$htmlOptions = array();
		}

		return $form->dropDownList($model, $attribute, $listData, $htmlOptions);
	}

	public static function getLanguageDropDownList($form, $model, $attribute, $htmlOptions = array()) {
		$listData = Html::listData(Language::model()->findAll(), 'code', 'value');

		if ($htmlOptions == null) {
			$htmlOptions = array();
		}

		$htmlOptions = array_merge($htmlOptions, array('empty' => array('*' => 'All language')));
		return $form->dropDownList($model, 'language', $listData, $htmlOptions);
	}

	public static function linkDetailProfile($data){
		$logedInUser = User::model()->findByPk(Yii::app()->user->id);
		//if(in_array($data->status, array(ProfileStatus::STATUS_TRANSLATED, ProfileStatus::STATUS_ACCEPTED, ProfileStatus::STATUS_WAITING_FOR_RETRANSLATE,ProfileStatus::STATUS_TRANSLATING,ProfileStatus::STATUS_PASS,ProfileStatus::STATUS_FLY, ProfileStatus::STATUS_STAY, ProfileStatus::STATUS_COMPLETE, ProfileStatus::STATUS_OUT)) && ($logedInUser->isManager() || $logedInUser->isEditor() || $logedInUser->isAdmin() || $logedInUser->isSuperAdmin())){
		if(!$logedInUser->isSubmitter())
		{
			return CHtml::link(CHtml::encode($data->full_name), Yii::app()->createUrl("translate/detail", array("id" => $data->id,"alias"=>stringURLSafe($data->full_name))));
		}else{
			return CHtml::link(CHtml::encode($data->full_name), Yii::app()->createUrl("profile/detail", array("id" => $data->id,"alias"=>stringURLSafe($data->full_name))));
		}
	}
	
	public static function buttonAddPartial($id)
	{
		return CHtml::button("+", array('class'=>'button add-partial', 'id'=>$id));
	}
	
	public static function customDropDownList($model, $id, $items, $valueField, $textField, $dataFields = array(), $firstItem, $style = '')
	{		
		$combo = CHtml::openTag('select', array('id' => get_class($model).'_'.$id, 'name'=>get_class($model).'['.$id.']', 'style'=>$style));
		if($firstItem != '')
		{
			$combo .= CHtml::tag('option', array('value'=>''), CHtml::encode($firstItem), true);
		}
		foreach ($items as $item){
			$selected = ($item->$valueField == $model->$id)? true : false;
			$textAttribute = '';
			if($dataFields !=null )
			{							
				foreach ($dataFields as $dataField){
					if (strpos($dataField,'->') !== false){
						$arr = explode('->', $dataField);
						if($item->$arr[0] instanceof CActiveRecord){
							$textAttribute .= $item->$arr[0]->$arr[1] . "||";
						}
					}else{
						$textAttribute .= $item->$dataField . "||";
					}
				}
			}
			$textAttribute = substr($textAttribute, 0, strlen($textAttribute) - 2); 
			
			$combo .= CHtml::tag('option', array('value' => $item->$valueField, 'data'=>$textAttribute, 'selected'=>$selected), CHtml::encode($item->$textField), true);
		}
		$combo .= CHtml::closeTag('select');
		return $combo;
	}
	
	public static function buttonDeleteItemInGrid($text = 'Delete',$title='Delete', $id= null, $class = '',$route = null, $params = array()) {
		$arrayDefault = array(
				'id' => $id,
				'title' => $title,
				'class' => 'button '.$class,
				'action' => url($route, $params));
		return CHtml::htmlButton($text, $arrayDefault);
	}
	
	public static function buttonDelete($id, $text = 'Xóa', $title = 'Xóa', $route = null, $params = array(), $htmlOptions = array()) {
		if($route == null)
			$route = app()->controller->id . '/delete';
		$event = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		if($params != null)
			$event = "$('#%s').attr('href', $('#%s').attr('action') + '&id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$event = sprintf($event, $id, $id);
	
		$onclick = "$('#%s').attr('href', $('#%s').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid','chkId'))";
		$onclick = sprintf($onclick, $id, $id);
		if($event != null)
			$onclick = $event;
		
		if ($htmlOptions == null) {
			$htmlOptions = array();
		}
		
		$arrayDefault = array(
				'data-update-dialog-title' =>'Lotus Ocean',
				'action' => url($route,$params),
				'onclick'=> $onclick,
				'live'=> '',
				'id' => $id,
				'title' => $title,
				'class' => 'button update-dialog-open-link');
		if(count($htmlOptions) == 0)
			return CHtml::htmlButton($text, $arrayDefault);
		else
		{
			$result = array_merge($arrayDefault, $htmlOptions);
			return CHtml::htmlButton($text, $result);
		}
	}
}

?>
